package com.rf.richfitwheel.workflow.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.TreeUtils;
import com.rf.richfitwheel.workflow.model.WfProcessFolder;
import com.rf.richfitwheel.workflow.service.WfProcessFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 流程目录表
 *
 * @author zhanglu
 * @email zhanglu2@cnpc.com.cn
 * @date 2021-07-20 09:01:22
 */
@RestController
@RequestMapping("/wfprocessfolder")
public class WfProcessFolderController {
    @Autowired
    private WfProcessFolderService wfProcessFolderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wfProcessFolderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        WfProcessFolder wfProcessFolder = wfProcessFolderService.getById(id);

        return R.ok().put("wfProcessFolder", wfProcessFolder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WfProcessFolder wfProcessFolder) {
        if (wfProcessFolder.getId() == null || "".equals(wfProcessFolder.getId())) {
            //新增
            wfProcessFolderService.addWfProcessFolder(wfProcessFolder, UserUtil.getUser());
        } else {
            //修改
            wfProcessFolderService.updateWfProcessFolder(wfProcessFolder, UserUtil.getUser());
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WfProcessFolder wfProcessFolder) {
        wfProcessFolderService.updateById(wfProcessFolder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        wfProcessFolderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/deleteById")
    public R deleteById(@RequestBody String id) {
        wfProcessFolderService.deleteById(id, UserUtil.getUser());
        return R.ok();
    }

    /**
     * 获取树形流程目录
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/treeListByIdForWfProcessFolder")
    @ResponseBody
    public List<TreeUtils> treeListByIdForProcessFolder(@RequestBody TreeUtils treeUtils) throws Exception {
        if (StringUtils.isBlank(treeUtils.getId())) {
            treeUtils.setId("0");
        }
        return wfProcessFolderService.getTreeForWfProcessFolder(treeUtils.getId());
    }
    
    /**
     * 获取树形流程目录，只返回对应前端模块的流程目录（包含系统）
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/treeListByIdAndModuleForWfProcessFolder")
    @ResponseBody
    public List<TreeUtils> treeListByIdAndModuleForWfProcessFolder(@RequestBody TreeUtils treeUtils, HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(treeUtils.getId())) {
            treeUtils.setId("0");
        }
        String moduleType = request.getHeader("moduleType") == null ? request.getHeader("Moduletype") : request.getHeader("moduleType"); //从请求头获取模块类型
        return wfProcessFolderService.getTreeForWfProcessFolderForModuleType(treeUtils.getId(), moduleType);
    }

    /**
     * 获取树形机构 包括部门
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/treeListByIdForWfModelExtend")
    @ResponseBody
    public List<TreeUtils> treeListByIdForWfModelExtend(@RequestBody TreeUtils treeUtils) throws Exception {
        if (StringUtils.isBlank(treeUtils.getId())) {
            treeUtils.setId("0");
        }
        return wfProcessFolderService.treeListByIdForWfModelExtend(treeUtils.getId());
    }

    /**
     * 获取按模块组织的流程定义
     * @return 流程定义
     */
    @GetMapping("/getWfProcessFolderForModuleType")
    public Map<String, List<WfProcessFolder>> getWfProcessFolderForModuleType(){
        return wfProcessFolderService.getWfProcessFolderForModuleType();
    }
}
